package com.kdd.campus.sh.listener;

import com.github.wxpay.sdk.WXPayUtil;
import com.kdd.campus.common.util.HttpClientUtil;
import com.kdd.campus.sh.po.ShOrder;
import com.kdd.campus.sh.service.OrderService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: fcp
 * @Date: 2020/5/23 09:15
 * @Description:
 */
@Component
public class RabbitmqListener {
    @Autowired
    private OrderService orderService;

    @Autowired
    private Environment environment;

    // 监听订单支付后的信息
    @RabbitListener(queues = "${mq.pay.order.queue}")
    public void listenerPayMessage(Map<String,String> message){
        System.out.println(message);

        if (message.get("return_code").equals("SUCCESS")){
            if (message.get("result_code").equals("SUCCESS")){
                // 交易成功
                String out_trade_no = message.get("out_trade_no");
                String time_end = message.get("time_end");
                // 修改订单状态
                orderService.updatePayStatus(out_trade_no,time_end);
            }else {
                // 交易失败，关闭支付，取消订单，库存回滚。什么情况下会交易失败？
                System.out.println(message.get("err_code"));
                System.out.println(message.get("err_code_des"));
            }
        }
    }

    // 监听订单超时的信息
    @RabbitListener(queues = "overOrderQueue")
    public void listenerOverOrder(Object message) throws Exception {
        ShOrder order = (ShOrder) message;
        // 关闭微信的订单      注意：订单生成后不能马上调用关单接口，最短调用时间间隔为5分钟。
//        https://api.mch.weixin.qq.com/pay/closeorder
        HashMap<String, String> map = new HashMap<>();
        map.put("appid", environment.getProperty("wechat.APPID"));
        map.put("mch_id", environment.getProperty("wechat.partner"));
        map.put("out_trade_no", order.getOrderSn());
        map.put("nonce_str", WXPayUtil.generateNonceStr());
        String xml = WXPayUtil.generateSignedXml(map, environment.getProperty("wechat.partnerkey"));

        String result = HttpClientUtil.doPostByXml("https://api.mch.weixin.qq.com/pay/closeorder", xml);
        Map<String, String> resultMap = WXPayUtil.xmlToMap(result);
        if (resultMap.get("return_code").equals("SUCCESS")){
            // 关闭订单成功
            if (resultMap.get("result_code").equals("SUCCESS")){
                // 修改订单状态
                order.setPayStatus(2);  //订单关闭
//                orderService.updatePayStatus(order);

                // 回滚商品库存

                return;
            }
            // 关闭订单失败，记录日志
            switch (resultMap.get("err_code")){
                case "ORDERPAID":
                    // 订单已支付，修改订单状态
                    break;
                case "SYSTEMERROR":
                    // 微信端系统错误，重新调用API
                    break;
            }

        }else {
            // 通信失败，记录日志

        }
    }



}
